//
//  ShareService.m
//  yilule
//
//  Created by lidi on 15/5/21.
//  Copyright (c) 2015年 topview. All rights reserved.
//

#import "ShareService.h"
#import "AFNetWorking.h"
#import <UShareUI/UShareUI.h>
#import "ServiceUserNew.h"

@implementation ShareService

+ (void)registerUMeng
{
    [UMConfigure setEncryptEnabled:YES];//打开加密传输
    [UMConfigure setLogEnabled:YES];//设置打开日志
    [UMConfigure initWithAppkey:UMAppKey channel:@"App Store"];
    [UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
    [UMSocialGlobal shareInstance].universalLinkDic = @{ @(UMSocialPlatformType_WechatSession):@"https://2vx6.ulml.mob.com/",
    @(UMSocialPlatformType_QQ):@"https://2vx6.ulml.mob.com/qq_conn/1104884296"
    };
}

+ (void)registerWechat
{
    /* 设置微信的appKey和appSecret */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WXAppId appSecret:WXAppSecret redirectURL:@"http://mobile.umeng.com/social"];
}

+ (void)registerSina
{
    /* 设置新浪的appKey和appSecret */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"1817405453"  appSecret:@"bfd665f1e6ebc86b01f999ca0204f92a" redirectURL:@"http://sns.whalecloud.com/sina2/callback"];
}

+ (void)registerQQ
{
    /* 设置分享到QQ互联的appID
     * U-Share SDK为了兼容大部分平台命名，统一用appKey和appSecret进行参数设置，而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
     */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQAppId/*设置QQ平台的appID*/  appSecret:QQAppKey redirectURL:@"http://mobile.umeng.com/social"];
}

+ (void)share:(UIViewController *)controller url:(NSString *)url title:(NSString *)title image:(NSString *)imageUrl completion:(UMSocialRequestCompletionHandler)completion {
    
    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_QQ),@(UMSocialPlatformType_Qzone),@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_WechatTimeLine)]];
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        // 根据获取的platformType确定所选平台进行下一步操作
        
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        
        //创建网页内容对象
        NSString* thumbURL = imageUrl;
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:title thumImage:thumbURL];
        //设置网页地址
        shareObject.webpageUrl = url;
        
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:controller completion:^(id data, NSError *error) {
            
            if (completion) {
                
                [ServiceUserNew doAddScore:AddScoreType_share
                                   success:^(id  _Nonnull responseObject, NSString * _Nonnull message) {}
                                   failure:^(NSString * _Nonnull message) {}];
                
                completion(data,error);
            }
            
            if (error) {
                UMSocialLogInfo(@"************Share fail with error %@*********",error);
            }else{
                if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                    UMSocialShareResponse *resp = data;
                    //分享结果消息
                    UMSocialLogInfo(@"response message is %@",resp.message);
                    //第三方原始返回的数据
                    UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                    
                }else{
                    UMSocialLogInfo(@"response data is %@",data);
                }
            }
        }];
        
    }];
}

@end
